package com.twu.biblioteca;

import java.util.ArrayList;

public class BookList {
    public ArrayList<Book> list;

    public BookList()
    {
        this.list = new ArrayList<Book>();
    }

    public void add(Book book)
    {
        if(book == null)
        {
            System.out.println("THERE WAS AN ERROR AND I DON'T KNOW STD PRACTICE FOR EXCEPTIONS YET");
            return;
        }

        list.add(book);

    }

    public String toString()
    {
        if(list.isEmpty())
            return "No books here!";

        String returnMe = "";
        for(Book book: list)
        {
            returnMe += book.toString() + "\n";
        }
        return returnMe;
    }

    public void Reserve(int index)
    {
        index = index - 1;
        list.get(index).reserve();
    }

    public String Reservable()
    {
        String returnMe = "";

        int i = 1;
        for(Book book: list)
        {
            if(!book.isReserved())
                returnMe += i + ". " + book.toString() + "\n";
            i++;
        }
        return returnMe.trim();
    }

    public boolean isReservable(int index)
    {
        index--;

        try {
            return ! list.get(index).isReserved();
        } catch (IndexOutOfBoundsException e) {
            return false;
        }
    }
}
